package com.zzccyy.jpdemo.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.WindowManager
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.prolificinteractive.materialcalendarview.CalendarDay
import com.prolificinteractive.materialcalendarview.DayViewDecorator
import com.prolificinteractive.materialcalendarview.DayViewFacade
import com.prolificinteractive.materialcalendarview.MaterialCalendarView
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.utils.ChineseWeekDayFormatter
import com.zzccyy.jpdemo.utils.TodayDayFormatter

class BtDateDialog1
/**
 * @param activity
 */(private val activity: Activity, private val onViewClickListener: OnViewClickListener?) : Dialog(
    activity, R.style.pop_product_detail_pop
) {
    constructor(activity: Activity, onViewClickListener: OnViewClickListener?, savedInstanceState: Bundle?) : this(activity, onViewClickListener)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.bt_dialog_date)
        setCancelable(true)
        val calendarView = findViewById<MaterialCalendarView>(R.id.calendarView)
        calendarView.setDayFormatter(TodayDayFormatter())
        calendarView.setWeekDayFormatter(ChineseWeekDayFormatter())
        val today = CalendarDay.today()

        // 2. 配置日期范围：最小日期为今天，无最大日期限制
        calendarView.state().edit()

            .commit() // 提交修改
        val tvC = findViewById<TextView>(R.id.tvC)
        val tvQ = findViewById<TextView>(R.id.tvQ)
        tvC.setOnClickListener { dismiss() }
        tvQ.setOnClickListener {
            if (onViewClickListener != null) {
                val s = (calendarView.selectedDate!!
                    .year.toString() + "年" + calendarView.selectedDate!!.month
                        + "月" + calendarView.selectedDate!!.day + "日")
                onViewClickListener.setData(s)
            }
            dismiss()
        }
        // 自定义标题格式器
        calendarView.setTitleFormatter { day -> // 获取年份
            val year = day.year
            val month = day.month
            // 拼接为 "年份 + 月份" 格式
            year.toString() + "年" + month + "月"
        }
        // 创建今日标记装饰器
        calendarView.selectedDate = CalendarDay.today()
        calendarView.addDecorator(TodayDecorator(activity))
    }


    override fun show() {
        super.show()
        val window = window
        window!!.setGravity(Gravity.BOTTOM)
        val params = window.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT //如果不设置,可能部分机型出现左右有空隙,也就是产生margin的感觉
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
        params.dimAmount = 0.5f //设置对话框的透明程度背景(非布局的透明度)

        window.attributes = params
        val lp = getWindow()!!.attributes
        getWindow()!!.attributes = lp
        super.show()
    }

    interface OnViewClickListener {
        fun setData(data: String?)
    }

    // 自定义装饰器类
    inner class TodayDecorator(context: Context?) : DayViewDecorator {
        private val today = CalendarDay.today()
        private val textColor = R.color.color_ff0000

        override fun shouldDecorate(day: CalendarDay): Boolean {
            return day == today
        }

        override fun decorate(view: DayViewFacade) {
            // 设置背景色
            // 设置文字颜色
            view.addSpan(ForegroundColorSpan(ContextCompat.getColor(activity, textColor)))
        }
    }
}
